package com.itheima.mp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.po.Address;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.AddressVO;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.enums.UserStatus;
import com.itheima.mp.mapper.UserMapper;
import com.itheima.mp.service.IUserService;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    public void deductBalance(Long id, Integer money) {
        // 1.查询用户
        User user = getById(id);
        // 2.判断用户状态
        if (user == null || user.getStatus() == UserStatus.FREEZE) {
            throw new RuntimeException("用户状态异常");
        }
        // 3.判断用户余额
        if (user.getBalance() < money) {
            throw new RuntimeException("用户余额不足");
        }
        // 4.扣减余额
        baseMapper.deductMoneyById(id, money);
    }
    public List<User> queryUsers(UserQuery query) {
        return lambdaQuery()
                .like(query.getUsername()!=null,User::getUsername, query.getUsername())
                .eq(query.getStatus()!=null,User::getStatus, query.getStatus())
                .ge(query.getMinBalance()!=null,User::getBalance, query.getMinBalance())
                .le(query.getMaxBalance()!=null,User::getBalance, query.getMaxBalance())
                .list();
    }
    public UserVO queryUserAndAddressesById(Long userId) {
        User user = getById(userId);
        if(user== null|| user.getStatus() == UserStatus.FREEZE){
            throw new RuntimeException("用户状态异常");
        }
        List<Address> addresses = Db.lambdaQuery(Address.class).eq(Address::getUserId, userId).list();
        UserVO userVO = BeanUtil.copyProperties(user, UserVO.class);
        if(addresses != null && !addresses.isEmpty()){
            userVO.setAddresses(BeanUtil.copyToList(addresses, AddressVO.class));
        }
        return userVO;
    }

    @Override
    public PageDTO<UserVO> queryUsersPage(UserQuery query) {
        // 1.构建条件
        Page<User> page = query.toMpPageDefaultSortByBalanceDesc();
        // 2.查询
        Page< User> p= lambdaQuery()
                .like(query.getUsername()!=null,User::getUsername, query.getUsername())
                .eq(query.getStatus()!=null,User::getStatus, query.getStatus())
                .page( page);

        // 3.封装返回
//        return PageDTO.of(p, UserVO.class);
        return PageDTO.of(page, user -> {
            // 拷贝属性到VO
            UserVO vo = BeanUtil.copyProperties(user, UserVO.class);
            // 用户名脱敏
            String username = vo.getUsername();
            vo.setUsername(username.substring(0, username.length() - 2) + "**");
            return vo;
        });
    }
}
